//
//  Toast.swift
//

import Toast_Swift

class Toast {
    static var keyWindow: UIWindow? {
        return UIApplication.shared.appWindow
    }
    
    static func loading(_ title: String = "") {
        hide()
        keyWindow?.makeToastActivity(title, isEnabled: true, position: .center)
    }
    
    static func maskLoading(_ title: String = "") {
        hide()
        keyWindow?.makeToastActivity(title, isEnabled: false, position: .center)
    }
    
    static func show(info: String, duration: TimeInterval = ToastManager.shared.duration) {
        hide()
        keyWindow?.makeToast(info, duration: duration, position: .center)
    }
    
    static func stateShow(info: String, duration: TimeInterval = ToastManager.shared.duration) {
        hideImmediately()
        DispatchQueue.main.async(execute: { () -> Void in
            keyWindow?.makeToast(info, duration: duration, position: .center)
        })
    }
    
    static func show(image: UIImage?, duration: TimeInterval = ToastManager.shared.duration) {
        hide()
        let imageView = UIImageView(image: image)
        keyWindow?.showToast(imageView, duration: duration)
    }
    
    static func show(customView: UIView, duration: TimeInterval = ToastManager.shared.duration) {
        hide()
        keyWindow?.showToast(customView, duration: duration, position: .center)
    }
    
    static func hideActivity() {
        keyWindow?.hideToastActivity()
    }
    
    static func hide() {
        keyWindow?.hideAllToasts(includeActivity: true)
    }
    
    static func hideImmediately() {
        DispatchQueue.main.async(execute: { () -> Void in
            hide()
        })
    }
}

